export function setCookie(name, value) {
  if (name && value) {
    const exp = new Date();
    exp.setTime(exp.getTime() + (value.expiresIn * 1000));
    // 字符串拼接
    const cookie = `${name}=${escape(value.accessToken)};expires=${exp.toGMTString()};path=/`;
    document.cookie = cookie;
  }
}

export function getCookie(name) {
  const reg = new RegExp(`(^| )${name}=([^;]*)(;|$)`);
  const match = document.cookie.match(reg);
  if (match) {
    return unescape(match[2]);
  }
  return null;
}


export function delCookie(name) {
  if (name) {
    const exp = new Date();
    exp.setTime(exp.getTime() - 1);
    const cval = getCookie(name);
    if (cval != null) {
      const cookie = `${name}=${cval};expires=${exp.toGMTString()}`;
      document.cookie = cookie;
    }
  }
}
export default { setCookie, getCookie, delCookie };
